<?php

namespace app\shop\controller\order;

use app\shop\controller\Controller;
use app\shop\model\order\Order as OrderModel;
use app\common\enum\settings\DeliveryTypeEnum;
use app\shop\model\settings\Express as ExpressModel;
use app\shop\model\store\Clerk as ShopClerkModel;
use think\facade\Request;
use think\facade\Db;

/**
 * 订单控制器
 */
class Order extends Controller
{
    /**
     * 订单列表
     */
	 
	     public function dellse()
    {
        try {
            // 获取POST数据
            $postData = $this->postData();
            
            // 验证必要参数
			
			
            if (empty($postData['category_id'])) {
                return $this->renderError('记录ID不能为空');
            }
            
            $id = intval($postData['category_id']);
            
            $record = Db::name('order')->where('order_id', $id)->find();
            if (!$record) {
                return $this->renderError('记录不存在');
            }
            
            // 执行删除操作
            $result = Db::name('jiedan_order')->where('id', $record['ding_id'])->delete();
            $result = Db::name('order')->where('order_id', $id)->delete();
            
            if ($result) {
                
                return $this->renderSuccess('删除成功');
            } else {
                return $this->renderError('删除失败');
            }
            
        } catch (\Exception $e) {
            error_log('删除记录错误: ' . $e->getMessage());
            return $this->renderError('删除失败: ' . $e->getMessage());
        }
    }
	 
    public function index($dataType = 'all')
    {
        // 订单列表
        $model = new OrderModel();
        $list = $model->getList($dataType, $this->postData());
        $order_count = [
            'order_count' => [
                'payment' => $model->getCount('payment'),
                'delivery' => $model->getCount('delivery'),
                'received' => $model->getCount('received'),
                'cancel' => $model->getCount('cancel'),
                'isyufu' => $model->getCount('isyufu'),
            ],];
        $ex_style = DeliveryTypeEnum::data();
        return $this->renderSuccess('', compact('list', 'ex_style', 'order_count'));
    }

    /**
     * 订单详情
     */
    public function detail($order_id)
    {
        // 订单详情
        $detail = OrderModel::detail($order_id);
        if (isset($detail['pay_time']) && $detail['pay_time'] != '') {
            $detail['pay_time'] = date('Y-m-d H:i:s', $detail['pay_time']);
        }
        if (isset($detail['delivery_time']) && $detail['delivery_time'] != '') {
            $detail['delivery_time'] = date('Y-m-d H:i:s', $detail['delivery_time']);
        }
        if ($detail['order_source'] == 70 && isset($detail['advance'])) {
            $detail['pay_price'] = round($detail['pay_price'] + $detail['advance']['pay_price'], 2);
            $detail['order_price'] = round($detail['order_price'] + $detail['advance']['pay_price'], 2);
        }
        $detail['buy_remark'] = json_decode($detail['buy_remark'], 1);
        // 物流公司列表 
        $model = new ExpressModel();
        $expressList = $model->getAll();
		
		$jiedan_order= Db::name('jiedan_order')->where(["id"=> ''. $detail['ding_id'].''])->select();
		$categoryfenleifen= Db::name('category')->where(["category_id"=> ''. $jiedan_order[0]['fenleifen'].''])->find();
		$categoryparentid= Db::name('category')->where(["category_id"=> ''. $jiedan_order[0]['fenleifen_index'].''])->find();
		
        // 门店店员列表
        $shopClerkList = (new ShopClerkModel)->getAll($detail['shop_supplier_id']);
        return $this->renderSuccess('', compact('detail', 'expressList', 'shopClerkList','jiedan_order','categoryfenleifen','categoryparentid'));
    }

    /**
     * 确认发货
     */
    public function delivery($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->delivery($this->postData('param'))) {
            return $this->renderSuccess('发货成功');
        }
        return $this->renderError('发货失败');
    }

    public function complete()
    {
        $postData = $this->postData();
        $order_id = intval($postData['order_id']);
        $model = OrderModel::detail($order_id);
        if (empty($model)){
            return $this->renderError('订单不存在');
        }
        // 验证用户
        Db::name('order')->where('order_id', $order_id)->update(['is_comment' => 1,'order_status' => 30]);
        $model = \app\api\model\order\Order::where(['order_id' => $order_id])->find();
        if ($model->delivery(['express_no' => $model['order_no'],'express_id' => $order_id,'order_id' => $order_id])) {
            return $this->renderSuccess('收货成功');
        }else{
            return $this->renderSuccess('OK');
        }
    }

 public function beizhudde($order_id)
    {
		
		
        $model = OrderModel::detail($order_id);
		
	
        if ($model->beizhudsxud($this->postData('param'))) {
			Db::name('beizhud_list')->insert(["order_id"=> ''. $order_id.'',"time"=> ''. time().'',"neirong"=> ''. $this->postData('param')['beizhudizho'].'',"user_id"=> ''. $model['user_id'].'',"order_no"=> ''. $model['order_no'].'']); 
			
            return $this->renderSuccess('设置成功');
        }
        return $this->renderError('设置失败');
    }
    /**
     * 修改订单价格
     */
    public function updatePrice($order_id)
    {
        $model = OrderModel::detail($order_id);
        if ($model->updatePrice($this->postData('order'))) {
            return $this->renderSuccess('修改成功');
        }
        return $this->renderError($model->getError() ?: '修改失败');
    }

    /**
     * 获取物流信息
     */
    public function express($order_id)
    {
        // 订单信息
        $order = OrderModel::detail($order_id);
        if (!$order['express_no']) {
            return $this->renderError('没有物流信息');
        }
        // 获取物流信息
        $model = $order['express'];
        $express = $model->dynamic($model['express_name'], $model['express_code'], $order['express_no']);
        if ($express === false) {
            return $this->renderError($model->getError());
        }
        return $this->renderSuccess('', compact('express'));
    }

    /**
     * 订单改地址
     */
    public function updateAddress($order_id)
    {
        // 订单信息
        $order = OrderModel::detail($order_id);
        if ($order['delivery_type'] == 10 && $order['delivery_status'] == 20) {
            return $this->renderError('订单已发货不允许修改');
        }
        // 获取物流信息
        $model = $order['address'];
        if (!$model->updateAddress($this->postData())) {
            return $this->renderError($model->getError() ?: '修改失败');
        }
        return $this->renderSuccess('修改成功');
    }

}